﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DapperExtensions;
using LmgCMS.Model;
namespace LmgCMS.Service
{
    ///<summary>
    ///新闻管理
    ///</summary>
    public interface INewsService : IRepository<News>
    {
        List<News> GetNewsLast(string category, int top);
        List<News> GetNewsLast(int top);
        List<News> GetNewsAll();
    }
    ///<summary>
    ///新闻管理
    ///</summary>
    public partial class NewsService : BaseRepository<News>, INewsService
    {
        public List<News> GetNewsLast(string category,int top)
        {
            if (top<=1)
            {
                top = 22;
            }
            string sql = $"SELECT TOP {top} * FROM [News] WHERE Category='{category}' ORDER BY AddTime DESC";
            if (string.IsNullOrWhiteSpace(category))
            {
                sql = $"SELECT TOP {top} * FROM [News] WHERE 1=1 ORDER BY AddTime DESC";
            }
            var list = DBA.Query<News>(sql);
            return list.ToList();
        }
        public List<News> GetNewsLast(int top)
        {
            string sql = $"SELECT TOP {top} * FROM [News] ORDER BY AddTime DESC";
            var list = DBA.Query<News>(sql);
            return list.ToList();
        }
        public List<News> GetNewsAll()
        {
            string sql = $"SELECT * FROM [News] ORDER BY AddTime DESC";
            var list = DBA.Query<News>(sql);
            return list.ToList();
        }
    }
}